package io.renren.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.renren.entity.VO.CustomBaseinfoVO;
import io.renren.service.CustomSettlementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.CustomBaseinfoEntity;
import io.renren.service.CustomBaseinfoService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;


/**
 * 客户主数据
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-15 20:58:29
 */
@RestController
@RequestMapping("custombaseinfo")
public class CustomBaseinfoController {

	/**
	 *  日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private CustomBaseinfoService customBaseinfoService;

	@Autowired
	private CustomSettlementService customSettlementService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("custombaseinfo:list")
	public R list(@RequestParam Map<String, Object> params){
		//与b1进行数据同步
		try {
			customSettlementService.startSynToB1();
		} catch (Exception e) {
 			//logger.info("与b1同步发生异常，数据可能不是最新的，请联系系统管理员！");
			logger.error(e.getMessage(),e);
			e.printStackTrace();
		}

		//查询列表数据
        Query query = new Query(params);

		List<CustomBaseinfoEntity> customBaseinfoList = customBaseinfoService.queryList(query);
		int total = customBaseinfoService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(customBaseinfoList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{unitId}")
	@RequiresPermissions("custombaseinfo:info")
	public R info(@PathVariable("unitId") String unitId){
		CustomBaseinfoEntity customBaseinfo = customBaseinfoService.queryObject(unitId);
		
		return R.ok().put("customBaseinfo", customBaseinfo);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("custombaseinfo:save")
	public R save(@RequestBody CustomBaseinfoEntity customBaseinfo){
		customBaseinfoService.save(customBaseinfo);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("custombaseinfo:update")
	public R update(@RequestBody CustomBaseinfoEntity customBaseinfo){
		customBaseinfoService.update(customBaseinfo);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("custombaseinfo:delete")
	public R delete(@RequestBody String[] unitIds){
		customBaseinfoService.deleteBatch(unitIds);
		
		return R.ok();
	}

	/**
	 * 获取树菜单
	 */
	@RequestMapping("/getTree")
	@RequiresPermissions("custombaseinfo:list")
	public R getTree(){
		List<CustomBaseinfoEntity> list = customBaseinfoService.getTree();
		List<CustomBaseinfoVO> res = new ArrayList<>();
		for (CustomBaseinfoEntity cus : list) {
			CustomBaseinfoVO vo = new CustomBaseinfoVO();
			vo.setName(cus.getUnitName());
			vo.setUnitId(cus.getUnitId());
			vo.setId(cus.getUnitId());
			res.add(vo);
		}
		return R.ok().put("list",res);
	}

	/**
	 * @return
	 */
	@RequestMapping("/getListinfo")
	@RequiresPermissions("custombaseinfo:select")
	public R getListinfo() {
		Map<String, Object> map = customBaseinfoService.getListinfo();
		return R.ok(map);
	}


	
}
